<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>杆塔管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">所属线路</label>
                    <div class="layui-input-block">
                        <select name="" lay-verify="required">
                            <option value=""></option>
                            <option value="0">ZY-00001自贡-宜宾线</option>
                            <option value="1">ZY-00002自贡-宜宾线</option>
                            <option value="2">ZY-00003自贡-宜宾线</option>
                            <option value="3">ZY-00004自贡-宜宾线</option>
                            <option value="4">ZY-00005自贡-宜宾线</option>
                            <option value="5">CY-00001成都-宜宾线</option>
                            <option value="6">CY-00002成都-宜宾线</option>
                            <option value="7">CY-00003成都-宜宾线</option>
                            <option value="8">CY-00004成都-宜宾线</option>
                            <option value="9">CY-00005成都-宜宾线</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否启用</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value="0">启用</option>
                            <option value="1">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-pole" lay-submit lay-filter="LAY-pole-front-search">
                        <i class="layui-icon layui-icon-search layui-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>
            <!--动态数据表格-->
            <table id="LAY-pole-manage" lay-filter="LAY-pole-manage"></table>
            <!--&lt;!&ndash;头像显示&ndash;&gt;-->
            <!--<script type="text/html" id="imgTpl">-->
            <!--<img style="display: inline-block; width: 50%; height: 100%;" src= {{ d.avatar }} onclick="showBigImage(this);">-->
            <!--</script>-->
            <!--状态判断-->
            <script type="text/html" id="statusTpl">
                {{#  if(d.status == 1){ }}
                <button class="layui-btn layui-btn-xs">启用</button>
                {{#  } else if(d.status == 2) { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">停用</button>
                {{#  } }}
            </script>

            <script type="text/html" id="table-pole-change">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="lock"><i class="layui-icon layui-icon-set"></i>切换状态</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="../../js/jquery-3.4.1.min.js"></script>
<script src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer','table','form','laydate'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;

        //加载table
        table.render({
            elem: '#LAY-pole-manage'
            ,url:'/pole/pagePole'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,title: 'tower_manage'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID'}
                ,{field:'pole_number', title:'塔杆编号'}
                ,{field:'circuit', title:'所属线路'}
                ,{field:'status', title:'状态（启用/停用）',template:'statusTpl'}
                ,{fixed: 'right', title:'操作', toolbar: '#table-pole-change',width:350}
            ]]
            ,page: true
        });


        //监听搜索
        form.on('submit(LAY-pole-front-search)', function(data){
            var field = data.field;


            //执行重载
            table.reload('LAY-user-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-polo-manage')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                //删除确定信息
                layer.confirm('确定删除吗？', function(index) {
                    //定义空数组
                    var ids = new Array();
                    $.each(checkData,function (index,item) {
                        //每循环一次，将id取出，塞给数组。
                        ids.push(item.id)
                    })
                    //通过Ajax访问后台实现删除功能
                    $.ajax({
                        url:"/circuit/deleteCircuit/"+ids,
                        type:"post",
                        traditional:true, //用传统的方式来序列化数据
                        success:function(res){
                            if(res.code == 200){
                                layer.msg(res.msg);
                                //执行重载
                                table.reload('LAY-polo-manage', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    ,where: {
                                        key: {
                                            id: new Date().getTime()
                                        }
                                    }
                                });
                            }
                            if(res.code == 500){
                                layer.msg(res.msg);
                            }
                        }
                    })
                    //表格重载
//                    table.reload('LAY-user-manage');
                });

            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加塔杆'
                    ,content: 'user/poloform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-pole-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            console.log(field)
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/pole/savePole",
                                 type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        //执行重载
                                        table.reload('LAY-pole-manage', {
                                            where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            table.reload('LAY-pole-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        //监听行工具事件
        table.on('tool(LAY-pole-manage)', function(obj){
            var data = obj.data;
//            console.log(data)
            if(obj.event === 'lock'){
                var data = obj.data;
                layer.confirm('确定修改状态吗？', function (index) {
                    //定义空数组
                    data.status=1-data.status;
                    $.ajax({
                        url: "/pole/updatePoleAll",
                        type: "post",
                        data:data,
                        dataType:"json",
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg);
                                console.log(data);
                                //执行重载
                                table.reload('LAY-pole-manage', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    , where: {
                                        key: {
                                            id: new Date().getTime()
                                        }
                                    },

                                });
                            }
                            if (res.code == 500) {
                                layer.msg(res.msg);
                            }
                        }
                    })

                });

            } else if(obj.event === 'edit'){
                console.log(111);
                layer.open({
                    type: 2
                    ,title: '修改用户'
                    ,content: 'user/editpoleform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,success: function(layero, index){//在页面加载成功之前将数据回显
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        console.log(body.html()) //得到iframe页的body内容
                        body.find('input[name=id]').val(data.id);
                        body.find('input[name=pole_number]').val(data.pole_number);

                        body.find('select option[value='+data.circuit+']').attr("selected","selected");
                        body.find('#status input[value='+data.status+']').attr("checked","checked");
                        //重载
                        iframeWin.layui.form.render("select");
                        iframeWin.layui.form.render("radio");


                    }
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-pole-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                           //console.log(field)
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/pole/updatePoleAll",
                                type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        //执行重载
                                        table.reload('LAY-pole-manage', {
                                            where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            table.reload('LAY-pole-front-submit'); //数据刷新

                            layer.close(index); //关闭弹层
                            console.log(field);
                        });

                        submit.trigger('click');
                    }
                });

            } else if(obj.event === 'del') {
                    var data = obj.data;
                    layer.confirm('确定删除吗？', function (index) {
                        //定义空数组
                        var ids = data.id;
                        //通过Ajax访问后台实现删除功能
                        $.ajax({
                            url: `/pole/deletePole/${ids}`,
                            type: "delete",
                            traditional: true, //用传统的方式来序列化数据
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.msg(res.msg);
                                    //执行重载
                                    table.reload('LAY-pole-manage', {
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                        , where: {
                                            key: {
                                                id: new Date().getTime()
                                            }
                                        }
                                    });
                                }
                                if (res.code == 500) {
                                    layer.msg(res.msg);
                                }
                            }
                        })

                    });
                }


        });

        $('.layui-btn.layuiadmin-btn-pole').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

</body>
</html>